﻿Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry

Public Class Calculation
    ''' <summary>
    ''' 二维点转三维点
    ''' </summary>
    ''' <param name="pt">二维点</param>
    ''' <returns>三维点</returns>
    ''' <remarks></remarks>
    Public Shared Function Pt2dTo3d(ByVal pt As Point2d) As Point3d
        Dim pt3d As Point3d = New Point3d(pt.X, pt.Y, 0)
        Return pt3d
    End Function

    ''' <summary>
    ''' 计算两点连线中点
    ''' </summary>
    ''' <param name="pt1">第1个点</param>
    ''' <param name="pt2">第2个点</param>
    ''' <returns>中点</returns>
    ''' <remarks></remarks>
    Public Shared Function MiddlePoint(ByVal pt1 As Point2d, _
                                       ByVal pt2 As Point2d) _
                           As Point2d

        Dim pt As Point2d = New Point2d((pt1.X + pt2.X) / 2, (pt1.Y + pt2.Y) / 2)
        Return pt

    End Function

    ''' <summary>
    ''' 计算两点连线中点
    ''' </summary>
    ''' <param name="pt1">第1个点</param>
    ''' <param name="pt2">第2个点</param>
    ''' <returns>中点</returns>
    ''' <remarks></remarks>
    Public Shared Function MiddlePoint(ByVal pt1 As Point3d, _
                                       ByVal pt2 As Point3d) _
                           As Point3d

        Dim pt As Point3d = New Point3d((pt1.X + pt2.X) / 2, _
                                        (pt1.Y + pt2.Y) / 2, _
                                        (pt1.Z + pt2.Z) / 2)
        Return pt

    End Function

    ''' <summary>
    ''' 弧度转化为角度
    ''' </summary>
    ''' <param name="angle">弧度</param>
    ''' <returns>角度</returns>
    ''' <remarks></remarks>
    Public Shared Function RtoG(ByVal angle As Double) As Double
        Return angle * 180 / Math.PI
    End Function

    ''' <summary>
    ''' 角度转化为弧度
    ''' </summary>
    ''' <param name="angle">角度</param>
    ''' <returns>弧度</returns>
    ''' <remarks></remarks>
    Public Shared Function GtoR(ByVal angle As Double) As Double
        Return angle * Math.PI / 180
    End Function

    ''' <summary>
    ''' 根据相对极坐标来确定一个点的位置
    ''' </summary>
    ''' <param name="pt">起始点</param>
    ''' <param name="angle">角度</param>
    ''' <param name="distance">相对距离</param>
    ''' <returns>新的点</returns>
    ''' <remarks></remarks>
    Public Shared Function PolarPoint(ByVal pt As Point3d, _
                                      ByVal angle As Double, _
                                      ByVal distance As Double) _
                           As Point3d

        Return New Point3d(pt.X + distance * Math.Cos(angle), _
                           pt.Y + distance * Math.Sin(angle), _
                           pt.Z)
    End Function

    ''' <summary>
    ''' 根据某点和相对位置求另一点
    ''' </summary>
    ''' <param name="pt">起始点</param>
    ''' <param name="x">X方向偏移量</param>
    ''' <param name="y">Y方向偏移量</param>
    ''' <returns>新的点</returns>
    ''' <remarks></remarks>
    Public Shared Function RelativePoint(ByVal pt As Point3d, _
                                         ByVal x As Double, _
                                         ByVal y As Double) _
                          As Point3d

        Return New Point3d(pt.X + x, pt.Y + y, pt.Z)

    End Function




End Class
